package org.example.manager.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.manager.TrafficManager;
import org.example.mapper.TrafficMapper;
import org.example.model.TrafficDO;
import org.example.util.TimeUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: 谢超
 * @date: 2024/9/3 10:32
 */
@Component
@Slf4j
public class TrafficManagerImpl implements TrafficManager {

    @Resource
    private TrafficMapper trafficMapper;

    @Override
    public int add(TrafficDO trafficDO) {
        return trafficMapper.insert(trafficDO);
    }

    @Override
    public IPage<TrafficDO> pageAvailable(int page, int size, long accountNo) {
        Page<TrafficDO> pageInfo = new Page<>(page, size);
        String today = TimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd");
        Page<TrafficDO> trafficDOPage = trafficMapper.selectPage(pageInfo, Wrappers.lambdaQuery(TrafficDO.class)
                .eq(TrafficDO::getAccountNo, accountNo)
                .ge(TrafficDO::getExpiredDate, today)
                .orderByDesc(TrafficDO::getGmtCreate));
        return trafficDOPage;
    }

    @Override
    public TrafficDO findByIdAndAccountNo(Long trafficId, long accountNo) {
        return trafficMapper.selectOne(Wrappers.lambdaQuery(TrafficDO.class)
                .eq(TrafficDO::getAccountNo, accountNo)
                .eq(TrafficDO::getId, trafficId));
    }

    @Override
    public boolean deleteExpireTraffic() {
        int row = trafficMapper.delete(Wrappers.lambdaQuery(TrafficDO.class).le(TrafficDO::getExpiredDate, new Date()));
        log.info("删除过期流量成功:{}", row);
        return true;
    }

    @Override
    public List<TrafficDO> selectAvailableTraffics(Long accountNo) {
        String today = TimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd");
        LambdaQueryWrapper<TrafficDO> queryWrapper = Wrappers.lambdaQuery();
        LambdaQueryWrapper<TrafficDO> wrapperTraffic = queryWrapper.eq(TrafficDO::getAccountNo, accountNo)
                .and(wrapper -> wrapper.ge(TrafficDO::getExpiredDate, today)).or().eq(TrafficDO::getOutTradeNo, "free_init");
        return trafficMapper.selectList(wrapperTraffic);
    }

    @Override
    public int addDayUsedTimes(Long accountNo, Long trafficId, Integer usedTimes) {
        return trafficMapper.addDayUsedTimes(accountNo, trafficId, usedTimes);
    }

    @Override
    public int releaseUsedTimes(Long accountNo, Long trafficId, Integer usedTimes, String userDateStr) {
        return trafficMapper.releaseUsedTimes(accountNo, trafficId, usedTimes, userDateStr);
    }

    @Override
    public int batchUpdateUsedTimes(Long accountNo, List<Long> unUpdatedTrafficIds) {
        return trafficMapper.update(null, Wrappers.lambdaUpdate(TrafficDO.class)
                .eq(TrafficDO::getAccountNo, accountNo)
                .in(TrafficDO::getId, unUpdatedTrafficIds)
                .set(TrafficDO::getDayUsed, 0));
    }
}
